<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门票名称" prop="ticketName">
        <el-input
          v-model="queryParams.ticketName"
          placeholder="请输入门票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门票编码" prop="ticketCode">
        <el-input
          v-model="queryParams.ticketCode"
          placeholder="请输入门票编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门票类型" prop="ticketType">
        <el-select v-model="queryParams.ticketType" placeholder="请选择门票类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.ticket_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bussiness:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column label="门票名称" align="center" prop="ticketName" />
      <el-table-column label="门票编码" align="center" prop="ticketCode" />
      <el-table-column label="购买人" align="center" prop="purchaseUser" />
      <el-table-column label="购买时间" align="center" prop="purchaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门票开始时间" align="center" prop="ticketStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ticketStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门票结束时间" align="center" prop="ticketEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ticketEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买金额" align="center" prop="purchaseMoney" />
      <el-table-column label="门票状态" align="center" prop="purchaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ticket_status" :value="scope.row.purchaseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="是否结算" align="center" prop="isPaid" :formatter="isPaidFormatter"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.purchaseStatus == 'notUsed'"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleVerification(scope.row)"
            v-hasPermi="['bussiness:ticket:verify']"
          >核销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase, verifyTicket } from "@/api/bussiness/purchase";

export default {
  name: "Purchase",
  dicts: ['ticket_type', 'ticket_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购票记录表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseUser: null,
        purchaseTime: null,
        purchaseStatus: null,
        isPaid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    isPaidFormatter(row){
      if(row.isPaid == 0){
        return '未结算'
      }else if(row.isPaid == 1){
        return '已结算'
      }
    },
    /** 查询购票记录列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseId: null,
        billId: null,
        purchaseUser: null,
        purchaseTime: null,
        ticketId: null,
        purchaseMoney: null,
        purchaseStatus: "0",
        isPaid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购票记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseId = row.purchaseId || this.ids
      getPurchase(purchaseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改购票记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.purchaseId != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 核销按钮操作 */
    handleVerification(row) {
      if(!row.purchaseId){
        return this.$modal.msgError("请选择需要核销的门票！");
      }
      this.$modal.confirm('是否确认核销当前门票？').then(function() {
        return verifyTicket(row.purchaseId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("核销成功");
      }).catch(() => {
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bussiness/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
